/*
 This file 'Tabbar' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2015 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2015-05-18 16:30
 */

define(function (require, exports, module) {

	require("Firebird.ui.Component");
	require('Firebird.lang.HashMap');
	require("Firebird.ui.Button");

	Firebird.define("Firebird.ui.Tabbar", {
		extend: "Firebird.ui.Component",
		statics: {
			uuid: function () {
				var s = [];
				var hexDigits = "0123456789abcdef";
				for (var i = 0; i < 36; i++) {
					s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
				}
				s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
				s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
				s[8] = s[13] = s[18] = s[23] = "-";
				return s.join("");
			},
			/**
			 * 属性适配器
			 * @param $component
			 * @returns {{}}
			 */
			attrs: function ($component) {
				return $.extend(true, {}, {
					renderTo: $component.get(0),
					instanceId: $component.attr("fb-instance") || $component.attr("id") || ("tabbar_" + Firebird.ui.Tabbar.uuid()),
					// active: $component.attr("fb-active") || $component.find("[fb-active=true]").attr("fb-target") || $component.find("[fb-target]:eq(0)").attr("fb-target") || "",
					autosize: Firebird.ui.Component.eval($component.attr("fb-autosize") || "false"),
					overflow: $component.attr("fb-overflow") || "auto",
					height: $component.attr("fb-height") || 310,
					width: $component.attr("fb-width") || "100%"
				});
			}
		},

		config: {
			renderTo: null,
			instanceId: null,
			active: "",
			autosize: false,
			height: 310,
			width: "100%",
			overflow: "auto"
		},

		instance: null,
		wrapper: null,
		wrapCls: "fb-ui-tabbar",

		tabStack: null,

		_init: function (config) {
			var me = this;

			this.tabStack = Firebird.create("Firebird.lang.HashMap");

			me.instance = $(me.getRenderTo()).addClass(this.wrapCls).data({
				instance: me,
				xtype: "tabbar"
			});

			this.render();

			this.setActive(me.instance.find("[fb-active=true]").attr("fb-tabid") || me.instance.find("[fb-target]:eq(0)").attr("fb-tabid") || "");

		},

		render: function () {
			var me = this;

			me.instance.find(">.fb-ui-tabbar-header [fb-target]").each(function (i, n) {
				var $tab = $(this);
				var tabid = $tab.attr("fb-tabid") || (function () {
						var uuid = Firebird.ui.Tabbar.uuid();
						$tab.attr("fb-tabid", uuid);
						return uuid;
					})();
				var $target = $($tab.attr("fb-target")) || "#notabfound";
				var autosize = $tab.attr("fb-autosize") ? Firebird.ui.Component.eval($tab.attr("fb-autosize")) : me.getAutosize();
				var height = $tab.attr("fb-height") ? $tab.attr("fb-height") : me.getHeight();
				var width = $tab.attr("fb-width") ? $tab.attr("fb-width") : me.getWidth();
				var overflow = $tab.attr("fb-overflow") ? $tab.attr("fb-overflow") : me.getOverflow();

				var css = {
					display: "none"
				};

				if (autosize === true) {
					css.overflow = overflow;
				} else {
					css.overflow = "auto";
					css.height = height;
					css.width = width;
				}

				if (css.overflow === "x") {
					css.overflow = "hidden";
					css.overflowX = "auto";
				}
				if (css.overflow === "y") {
					css.overflow = "hidden";
					css.overflowY = "auto";
				}


				$target.css(css);

				me.tabStack.put(tabid, {
					id: tabid,
					tab: $tab,
					target: $target,
					autosize: autosize,
					height: height,
					width: width
				});

				$tab.on("click", function (e) {
					var $tab = $(this);
					setTimeout(function () {
						me.setActive($tab.attr("fb-tabid"));
					}, 100);
					e.preventDefault();
				});
			});


			// me.instance.on("click", "[fb-target]", function (e) {
			// 	var $tab = $(this);
			// 	setTimeout(function () {
			// 		me.setActive($tab.attr("fb-tabid"));
			// 	}, 100);
			// 	e.preventDefault();
			// });
		},

		_unActiveAll: function () {
			var me = this;
			$.each(me.tabStack.getMap(), function (i, n) {
				n.target.css({
					display: "none"
				});
				n.tab.removeClass("active");
			});
		},

		applyActive: function (id) {
			var me = this,
				tab = me.tabStack && me.tabStack.get(id);
			if (me.instance && tab) {
				me._unActiveAll();
				tab.tab.addClass("active");
				tab.target.css({
					display: "block"
				}).trigger("onActive");
				me.instance.trigger("onFocus", tab);
			}
			return id;
		},

		add: function (tab) {
			tab = $.extend(true, {}, {
				label: "",
				text: "",
				icon: ""
			}, tab);

			var tabButton = $("<a></a>");

			this.instance.append(tabButton);

		},
		getValue: function () {
			return "";
		},
		setValue: function (value) {

		}
	}, function () {

		//Firebird.ui.Tabbar.created();
	});

	module.exports = Firebird.ui.Tabbar;
});